package com.bookmanger.server.service.impl;

import com.bookmanger.common.exception.BaseException;
import com.bookmanger.pojo.DTO.*;
import com.bookmanger.pojo.Entity.*;
import com.bookmanger.pojo.VO.BorrowVO;
import com.bookmanger.pojo.VO.PrebookVO;
import com.bookmanger.server.mapper.ReaderMapper;
import com.bookmanger.server.service.ReaderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class ReaderServiceImpl implements ReaderService {

    @Autowired
    private ReaderMapper readerMapper;

    @Override
    public Reader updateself(Reader reader) {
        if (reader == null || reader.getId() == null) {
            throw new BaseException("参数错误：缺少读者ID");
        }

        Reader exists = readerMapper.getreader(reader.getId());
        if (exists == null) {
            throw new BaseException("读者不存在");
        }

        boolean hasUpdatableField =
                reader.getName() != null ||
                reader.getDepartment() != null ||
                reader.getMajor() != null ||
                reader.getGrade() != null ||
                reader.getPhoneNumber() != null ||
                reader.getEmail() != null ||
                reader.getGender() != null;

        if (!hasUpdatableField) {
            throw new BaseException("无可更新的字段");
        }

        readerMapper.updateself(reader);
        return readerMapper.getreader(reader.getId());
    }

    @Override
    public List<BorrowVO> getborrows(int readerId) {
        return readerMapper.getborrows(readerId);
    }

    @Override
    public List<BorrowVO> getLossBorrows(int readerId) {
        return readerMapper.getLossBorrows(readerId);
    }

    @Override
    public List<BorrowVO> getReturnedUnapprovedBorrows(int readerId) {
        return readerMapper.getReturnedUnapprovedBorrows(readerId);
    }

    @Override
    public Renew renew(RenewDTO renew) {
        readerMapper.renew(renew);
        return null;
    }

    @Override
    public PageInfo<BorrowVO> findBorrowRecords(FindRecordDTO findRecordDTO) {
        int page = findRecordDTO.getPage() == null || findRecordDTO.getPage() < 1 ? 1 : findRecordDTO.getPage();
        int pageSize = findRecordDTO.getPageSize() == null || findRecordDTO.getPageSize() < 1 ? 10 : findRecordDTO.getPageSize();
        // 限制每页最大记录数
        if (pageSize > 100) pageSize = 100;
        PageHelper.startPage(page, pageSize, true);
        List<BorrowVO> list = readerMapper.findBorrowRecords(findRecordDTO);
        return new com.github.pagehelper.PageInfo<>(list);
    }

    @Override
    public void prebook(PrebookDTO prebookDTO) {
        readerMapper.prebook(prebookDTO);
    }

    @Override
    public List<PrebookVO> getPrebooks(int readerId) {
        return readerMapper.getPrebooks(readerId);
    }

    @Override
    public void cancelPrebook(int id) {
        readerMapper.cancelPrebook(id);
    }

    @Override
    public PageInfo<PrebookVO> getallPrebooks(FindPrebookDTO findPrebookDTO) {
        int page = findPrebookDTO.getPage() == null || findPrebookDTO.getPage() < 1 ? 1 : findPrebookDTO.getPage();
        int pageSize = findPrebookDTO.getPageSize() == null || findPrebookDTO.getPageSize() < 1 ? 10 : findPrebookDTO.getPageSize();
        // 限制每页最大记录数
        if (pageSize > 100) pageSize = 100;
        PageHelper.startPage(page, pageSize, true);
        List<PrebookVO> list = readerMapper.getallPrebooks(findPrebookDTO);
        return new com.github.pagehelper.PageInfo<>(list);
    }

    @Override
    public void lossbook(int readerId, int bookId) {
        readerMapper.lossbook(readerId,bookId);
    }

    @Override
    public void cancellossbook(int readerId, int bookId)
    {
        readerMapper.cancellossbook(readerId,bookId);
    }

    @Override
    public List<Book> getbooks() {
        return readerMapper.getbooks();
    }

    @Override
    public void changepassword(PasswordDTO passwordDTO) {
        Reader reader = readerMapper.getreader(passwordDTO.getReaderId());
        String oldpwd = DigestUtils.md5DigestAsHex(passwordDTO.getOldpassword().getBytes());
        String newpwd = DigestUtils.md5DigestAsHex(passwordDTO.getNewpassword().getBytes());
        if (!reader.getPassword().equals(oldpwd)) {
            throw new BaseException("原始密码错误");
        }
        passwordDTO.setNewpassword(newpwd);
        readerMapper.changepassword(passwordDTO);
    }

    @Override
    public Reader getself(int readerId) {
        return readerMapper.getreader(readerId);
    }

    @Override
    public void returnbook(int readerId, int bookId) {
        readerMapper.returnbook(readerId,bookId);
    }

    @Override
    public void returnbookyes(int readerId, int bookId) {
        readerMapper.returnbookyes(readerId,bookId);
    }
}


